<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Agp\CoreBundle\Validator\Constraints as CoreAssert;
use Doctrine\Common\Collections\ArrayCollection;
use APY\DataGridBundle\Grid\Mapping as GRID;

/**
 * Agp\CoreBundle\Entity\SolicitacaoRepasse
 *
 * @ORM\Table(name="solicitacao_repasse")
 * @ORM\Entity(repositoryClass="Agp\CoreBundle\Repository\SolicitacaoRepasseRepository")
 * 
 * @GRID\Source(groups="cliente", groupBy="codSolicitacaoRepasse", columns="codSolicitacaoRepasse, responsavel.nome, dt, situacao, vlrSolicitado, repasses.vlrSolicitado:sum, repasses.vlrRepassado:sum")
 * @GRID\Source(groups="admin", groupBy="codSolicitacaoRepasse", columns="codSolicitacaoRepasse, responsavel.nome, situacao, vlrSolicitado, repasses.vlrSolicitado:sum, repasses.vlrRepassado:sum")
 * @CoreAssert\RepasseVlrDisponivel
 * 
 */
class SolicitacaoRepasse
{
    /**
     * @var integer $codSolicitacaoRepasse
     *
     * @ORM\Column(name="cod_solicitacao_repasse", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * 
     * @GRID\Column(title="Código", type="text", size="5", primary=true, visible=false)
     */
    protected $codSolicitacaoRepasse;
    
    /**
     * @var object $empresa
     *
     * @ORM\ManyToOne(targetEntity="Empresa", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_empresa", referencedColumnName="cod_empresa", onDelete="Cascade", nullable=false)
     */
    protected $empresa;
            
    /**
     * @var string $observacao
     *
     * @ORM\Column(name="observacao", type="text", nullable=true)
     * 
     * @GRID\Column(title="Observação", type="text")
     */
    protected $observacao;
    
    /**
     * @var object $responsavel
     *
     * @ORM\ManyToOne(targetEntity="Pessoa", fetch="LAZY")
     * @ORM\JoinColumn(name="cod_pessoa", referencedColumnName="cod_pessoa", onDelete="Cascade", nullable=false)
     * @Assert\NotBlank(groups={"agape"})
     * 
     * @GRID\Column(field="responsavel.nome", title="Responsável", type="text")
     */
    protected $responsavel;  
    
    /**
     *
     * @var datetime
     * 
     * @ORM\Column(type="datetime", nullable=false)
     * 
     * @GRID\Column(title="Data", type="datetime", align="center")
     */
    protected $dt;
    
    /**
     * @var float $vlr
     *
     * @ORM\Column(name="vlr_solicitado", type="float", nullable=true)
     * @Assert\NotBlank
     * 
     * @GRID\Column(title="Valor Solicitado", type="number", style="currency")
     */
    protected $vlrSolicitado;
    
    const SIT_ATIVO = 'Ativo';
    const SIT_PENDENTE = 'Pendente';
    const SIT_ULTIMO = 'Ultimo';
    
    /**
     * @var string $situacao
     *
     * @ORM\Column(name="situacao", type="string", nullable=false)
     * 
     * @GRID\Column(title="Situação", type="text", align="center", filter="select", selectFrom="source")
     */
    protected $situacao;
    
    /**
     *
     * @Grid\Column(align="center", type="number", style="currency", field="repasses.vlrSolicitado:sum", title="Valor Solicitado (corrigido)")
     * @Grid\Column(align="center", type="number", style="currency", field="repasses.vlrRepassado:sum", title="Valor Repassado")
     * 
     * @ORM\OneToMany(targetEntity="Agp\CoreBundle\Entity\Repasse", mappedBy="solicitacaoRepasse")
     * 
     */
    protected $repasses;
    
    /**
     *
     * @ORM\OneToMany(targetEntity="Agp\CoreBundle\Entity\Pedido", mappedBy="solicitacaoRepasse")
     * 
     */
    protected $pedidos;
    
    public function __construct() {
        $this->dt = new \DateTime;
        $this->situacao = self::SIT_PENDENTE;
        $this->vlrSolicitado = 0;
        $this->vlrRepassado = 0;
        $this->ultimo = 0;
        $this->repasses = new ArrayCollection;
    }
    
    public function vlrTotalRepassado(){
        $valor = 0;
        foreach ($this->repasses as $r){
            $valor += $r->getVlrRepassado();
        }
        
        return $valor;
    }

    public function getCodSolicitacaoRepasse() {
        return $this->codSolicitacaoRepasse;
    }

    public function setCodSolicitacaoRepasse($codSolicitacaoRepasse) {
        $this->codSolicitacaoRepasse = $codSolicitacaoRepasse;
    }

    public function getEmpresa() {
        return $this->empresa;
    }

    public function setEmpresa($empresa) {
        $this->empresa = $empresa;
    }

    public function getDt() {
        return $this->dt;
    }

    public function setDt($dt) {
        $this->dt = $dt;
    }

    public function getResponsavel() {
        return $this->responsavel;
    }

    public function setResponsavel($responsavel) {
        $this->responsavel = $responsavel;
    }
    
    public function getSituacao() {
        return $this->situacao;
    }

    public function setSituacao($situacao) {
        $this->situacao = $situacao;
    }
    
    public function getVlrSolicitado() {
        return $this->vlrSolicitado;
    }

    public function setVlrSolicitado($vlrSolicitado) {
       $this->vlrSolicitado = substr(str_replace(",", ".", $vlrSolicitado),3);
            
    }
    
    public function setVlrSolicitadoNotSubstring($vlrSolicitado){
        $this->vlrSolicitado = $vlrSolicitado;
    }

    public function getVlrRepassado() {
        return $this->vlrRepassado;
    }

    public function setVlrRepassado($vlrRepassado) {
        $this->vlrRepassado = $vlrRepassado;
    }
    
    public static function getSituacaoList(){
        return array(self::SIT_ATIVO => self::SIT_ATIVO,
                     self::SIT_PENDENTE => self::SIT_PENDENTE);
    }
    
    public function getRepasses() {
        return $this->repasses;
    }

    public function setRepasses($repasses) {
        $this->repasses = $repasses;
    }
    
    public function getObservacao() {
        return $this->observacao;
    }

    public function setObservacao($observacao) {
        $this->observacao = $observacao;
    }
    
    public function getPedidos() {
        return $this->pedidos;
    }

    public function setPedidos($pedidos) {
        $this->pedidos = $pedidos;
    }

}
